package handler

import (
	"context"
	"examination-management/srv/cmd/config"
	"examination-management/srv/cmd/model"
	pb "examination-management/srv/proto"
)

func (s *Server) CreateTestModule(_ context.Context, in *pb.CreateTestModuleReq) (*pb.CreateTestModuleResp, error) {
	var u model.User
	config.DB.Debug().Where("id = ?", in.UId).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.CreateTestModuleResp{
			Message: "未登录",
			Code:    10001,
		}, nil
	}
	var m model.TestManagement
	config.DB.Debug().Where("id = ?", in.TestManagementId).Limit(1).Find(&m)
	if m.Id == 0 {
		return &pb.CreateTestModuleResp{
			Message: "试题不存在",
			Code:    10002,
		}, nil
	}
	var founder model.User
	config.DB.Debug().Where("id = ?", in.FounderId).Limit(1).Find(&founder)
	if founder.Id == 0 {
		return &pb.CreateTestModuleResp{
			Message: "用户不存在",
			Code:    100013,
		}, nil
	}
	var o model.TestOption
	config.DB.Debug().Where("id = ?", in.TestOptionId).Limit(1).Find(&o)
	if o.TestManagementId != in.TestManagementIds {
		return &pb.CreateTestModuleResp{
			Message: "选项不存在",
			Code:    10004,
		}, nil
	}
	if o.TestManagementId != m.Id {
		return &pb.CreateTestModuleResp{
			Message: "题型不符",
			Code:    10005,
		}, nil
	}
	module := model.TestModule{}
	if u.UType != "管理员" {
		module = model.TestModule{
			TestManagementId:     m.Id,
			TestQuestionType:     m.TestQuestionType,
			Difficulty:           m.Difficulty,
			ProfessionalScopeOne: m.ProfessionalScopeOne,
			ProfessionalScopeTwo: m.ProfessionalScopeTwo,
			UId:                  u.Id,
			Author:               u.Name,
			AuthorCard:           u.IdCard,
			FounderId:            founder.Id,
			Founder:              founder.Name,
			FounderCard:          founder.IdCard,
			TestQuestions:        m.TestQuestions,
			TestOptionId:         o.Id,
			TestManagementIds:    o.TestManagementId,
			Option:               o.Option,
			Select:               o.Select,
			TestAnswer:           in.TestAnswer,
			Analysis:             in.Analysis,
			Keywords:             in.Keywords,
			ViewingRange:         in.ViewingRange,
			AuditStatus:          in.AuditStatus,
		}
	} else {
		module = model.TestModule{
			TestManagementId:     m.Id,
			TestQuestionType:     m.TestQuestionType,
			Difficulty:           m.Difficulty,
			ProfessionalScopeOne: m.ProfessionalScopeOne,
			ProfessionalScopeTwo: m.ProfessionalScopeTwo,
			UId:                  u.Id,
			Author:               u.Name,
			AuthorCard:           u.IdCard,
			FounderId:            founder.Id,
			Founder:              founder.Name,
			FounderCard:          founder.IdCard,
			TestQuestions:        m.TestQuestions,
			TestOptionId:         o.Id,
			TestManagementIds:    o.TestManagementId,
			Option:               o.Option,
			Select:               o.Select,
			TestAnswer:           in.TestAnswer,
			Analysis:             in.Analysis,
			Keywords:             in.Keywords,
			ViewingRange:         in.ViewingRange,
			AuditStatus:          "已审核",
		}
	}

	err := config.DB.Debug().Create(&module).Error
	if err != nil {
		return &pb.CreateTestModuleResp{
			Message: "添加失败",
			Code:    500,
		}, nil
	}
	return &pb.CreateTestModuleResp{
		Message: "添加成功",
		Code:    200,
	}, nil
}

func (s *Server) TestModuleInfo(_ context.Context, in *pb.TestModuleInfoReq) (*pb.TestModuleInfoResp, error) {
	var u model.User
	config.DB.Debug().Where("id = ?", in.UId).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.TestModuleInfoResp{
			Message: "未登录",
			Code:    10001,
		}, nil
	}
	var m []model.TestModule
	err := config.DB.Debug().Where("id = ?", in.Id).Find(&m).Error
	if err != nil {
		return &pb.TestModuleInfoResp{
			Message: "查询失败",
			Code:    10002,
		}, nil
	}
	var list []*pb.TestModuleInfoList
	for _, module := range m {
		list = append(list, &pb.TestModuleInfoList{
			TestQuestionType:     module.TestQuestionType,
			Difficulty:           module.Difficulty,
			ProfessionalScopeOne: module.ProfessionalScopeOne,
			ProfessionalScopeTwo: module.ProfessionalScopeTwo,
			Author:               module.Author,
			Founder:              module.Founder,
			TestQuestions:        module.TestQuestions,
			Option:               module.Option,
			Select:               module.Select,
			TestAnswer:           module.TestAnswer,
			Analysis:             module.Analysis,
			Keywords:             module.Keywords,
			ViewingRange:         module.ViewingRange,
			AuditStatus:          module.AuditStatus,
		})
	}
	return &pb.TestModuleInfoResp{
		Message: "展示成功",
		Code:    200,
		List:    list,
	}, nil
}
